from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

import db
import base64
from polls import poll
from polloptions import polloption
from pollresponses import pollresponse

class pollcontroller(object):
	lookup = None
	def __init__(self, lup ):
		self.lookup = lup

	def index(self, msg=None):
		sess = db.getsession()
		p = sess.query(poll).all()
		return self.lookup.get_template("polls.html").render(polls=p, msg=msg)
	index.exposed = True

	def deleteoption(self, optionid, pollid):
		sess = db.getsession()
		try:
			opt = sess.query(polloption).filter(polloption.OPTIONID == optionid).one()
			sess.delete(opt)
			sess.flush()
		except Exception, e:
			print "Failed to Delete", e
		return self.displaypolloptions(pollid = pollid)
	deleteoption.exposed = True

	def deletepoll(self, pollid):
		sess = db.getsession()
		try:
			p = sess.query(poll).filter(poll.POLLID == pollid).one()
			sess.delete(p)
			sess.flush()
		except Exception, e:
			print "Failed to Delete", e
		return self.index()
	deletepoll.exposed = True

	def displaypolloptions(self, pollid):
		sess = db.getsession()
		try:
			opts = sess.query(polloption).filter(polloption.POLLID == pollid).all()
		except:
			opts = None
		return self.lookup.get_template("listpolloptions.html").render(popts=opts)

	displaypolloptions.exposed = True

	def editpoll(self, pollid=None):
		sess = db.getsession()
		if pollid:
			ps = sess.query(poll).filter(poll.POLLID == pollid).one()
			opts = sess.query(polloption).filter(polloption.POLLID == pollid).all()
			return self.lookup.get_template("editpoll.html").render(p = ps, popts=opts, table = db.poll_table)
		else:
			ps = poll()
			return self.lookup.get_template("editpoll.html").render(p = ps, popts=None, table=db.poll_table)
	editpoll.exposed = True

	def currentpoll(self):
		sess = db.getsession()
		try:
			try:
				ps = sess.query(poll).order_by("POLLID").one()
			except:
				ps = None
			try:
				opts = sess.query(polloption).filter(polloption.POLLID == ps.POLLID).all()
			except:
				opts = None
			return self.lookup.get_template("displaypolls.html").render(p=ps, poptions = opts)
		except Exception, e:
			return self.lookup.get_template("displaypolls.html").render(p=None, poptions=None)
	currentpoll.exposed = True	

	def answerpoll(self, optionid):
		sess = db.getsession()
		presp = pollresponse()
		presp.OPTIONID = optionid
		pol = None
		panswers = None
		try:
			pollid = (sess.query(polloption).filter(polloption.OPTIONID == optionid).one()).POLLID
			sess.save(presp)
			sess.flush()
		except Exception, e:
			print "Could not save Response ",e
		try:
			pol = sess.query(poll).filter(poll.POLLID == pollid).one()
			opts = sess.query(polloption).filter(polloption.POLLID == pollid).all()
			answers = []
			for o in opts:
				answers.append([o, sess.query(pollresponse).filter(pollresponse.OPTIONID == o.OPTIONID).count()])
		except Exception, e:
			print "Failed to prep ", e
		return self.lookup.get_template("pollanswers.html").render(p=pol, panswers=answers)
	answerpoll.exposed = True

	def saveanswer(self, answer, pollid):
		sess = db.getsession()
		popt = polloption()
		popt.ANSWER = answer
		popt.POLLID = pollid
		sess.save(popt)
		sess.flush()
		return self.displaypolloptions(pollid=pollid)
	saveanswer.exposed = True

	def savepoll(self, POLLID, QUESTION):
		sess = db.getsession()
		try:
			p = sess.query(poll).filter(poll.POLLID == pollid).one()
		except:
			p = poll()
			sess.save(p)
		p.QUESTION = QUESTION
		try:
			sess.flush()
			msg = "Poll saved"
		except Exception, e:
			msg = "Failed to save poll " + str(e)
		return self.index(msg=msg)
	savepoll.exposed = True
